package com.qidi.web;

import com.google.gson.Gson;
import com.qidi.pojo.Book;
import com.qidi.pojo.Cart;
import com.qidi.pojo.CartItem;
import com.qidi.service.BookService;
import com.qidi.service.impl.BookServiceimpl;
import com.qidi.utils.WebUtils;
import sun.java2d.pipe.SpanIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zhouXinBin
 * @create 2020-09-03-16:46
 */
public class CartServlet extends BaseServlet {
    BookService bookService=new BookServiceimpl();
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bookid = WebUtils.parseInt(request.getParameter("bookid"), 0);
        Book book = bookService.queryBookById(bookid);
        request.getSession().setAttribute("lastname",book.getName());
        CartItem item = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart==null){
            cart = new Cart();
            cart.addItem(item);
            request.getSession().setAttribute("cart",cart);
        }else {
            cart.addItem(item);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void AjaxaddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bookid = WebUtils.parseInt(request.getParameter("bookid"), 0);
        Book book = bookService.queryBookById(bookid);
        //request.getSession().setAttribute("lastname",book.getName());
        CartItem item = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart==null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(item);

        request.getSession().setAttribute("lastname",book.getName());
        //response.sendRedirect(request.getHeader("Referer"));

        Map<String,Object> ResultMap=new HashMap<>();
        ResultMap.put("lastname",book.getName());
        ResultMap.put("totalcounts",cart.getTotalProductNumber());
        Gson gson=new Gson();
        String Resultjson = gson.toJson(ResultMap);
        response.getWriter().write(Resultjson);
    }




    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int itemid = WebUtils.parseInt(request.getParameter("itemid"), 0);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.deleteItem(itemid);
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void clearItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart= (Cart) request.getSession().getAttribute("cart");
        if (cart!=null){
            cart.clearCart();
        }
        response.sendRedirect(request.getContextPath()+"/pages/cart/cart.jsp");
    }

    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtils.parseInt(request.getParameter("itemid"), 0);
        int count = WebUtils.parseInt(request.getParameter("count"), 1);
        Cart cart= (Cart) request.getSession().getAttribute("cart");

        if (cart!=null){
            cart.updateCount(id,count);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }
}
